// Copyright (c) 2023, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import "package:expect/expect.dart";

// More tests that map literals are ordered.

@pragma('dart2js:never-inline')
@pragma('vm:never-inline')
void check<K, V>(String expectedKeys, String expectedValues, Map<K, V> map) {
  Expect.equals(expectedKeys, map.keys.join(','));
  Expect.equals(expectedValues, map.values.join(','));

  Expect.equals(expectedKeys, List.of(map.keys).join(','));
  Expect.equals(expectedValues, List.of(map.values).join(','));
}

main() {
  check('', '', const {});
  check('1,2', '10,20', const {1: 10, 2: 20});
  check('2,1', '20,10', const {2: 20, 1: 10});

  // Integer keys.
  check('100,20,3', '1,2,3', const {100: 1, 20: 2, 3: 3});
  check('100,3,20', '1,3,2', const {100: 1, 3: 3, 20: 2});
  check('20,100,3', '2,1,3', const {20: 2, 100: 1, 3: 3});
  check('20,3,100', '2,3,1', const {20: 2, 3: 3, 100: 1});
  check('3,100,20', '3,1,2', const {3: 3, 100: 1, 20: 2});
  check('3,20,100', '3,2,1', const {3: 3, 20: 2, 100: 1});

  // Strings with integer-like values.
  check('100,20,3', '1,2,3', const {'100': 1, '20': 2, '3': 3});
  check('100,3,20', '1,3,2', const {'100': 1, '3': 3, '20': 2});
  check('20,100,3', '2,1,3', const {'20': 2, '100': 1, '3': 3});
  check('20,3,100', '2,3,1', const {'20': 2, '3': 3, '100': 1});
  check('3,100,20', '3,1,2', const {'3': 3, '100': 1, '20': 2});
  check('3,20,100', '3,2,1', const {'3': 3, '20': 2, '100': 1});

  // ASCII order
  check('100,200,30,40,5,6', '1,2,3,4,5,6',
      const {'100': 1, '200': 2, '30': 3, '40': 4, '5': 5, '6': 6});
  // Reverse ASCII order
  check('6,5,40,30,200,100', '6,5,4,3,2,1',
      const {'6': 6, '5': 5, '40': 4, '30': 3, '200': 2, '100': 1});
  // Numeric order
  check('5,6,30,40,100,200', '5,6,3,4,1,2',
      const {'5': 5, '6': 6, '30': 3, '40': 4, '100': 1, '200': 2});
  // Prefix in numeric order
  check('5,6,100,200,30,40', '5,6,1,2,3,4',
      const {'5': 5, '6': 6, '100': 1, '200': 2, '30': 3, '40': 4});

  // String keys, mixed integer-like and other.
  check('100,20,3,a', '1,2,3,z', const {'100': 1, '20': 2, '3': 3, 'a': 'z'});
  check('100,3,20,a', '1,3,2,z', const {'100': 1, '3': 3, '20': 2, 'a': 'z'});
  check('20,100,3,a', '2,1,3,z', const {'20': 2, '100': 1, '3': 3, 'a': 'z'});
  check('20,3,100,a', '2,3,1,z', const {'20': 2, '3': 3, '100': 1, 'a': 'z'});
  check('3,100,20,a', '3,1,2,z', const {'3': 3, '100': 1, '20': 2, 'a': 'z'});
  check('3,20,100,a', '3,2,1,z', const {'3': 3, '20': 2, '100': 1, 'a': 'z'});

  check('a,100,20,3', 'z,1,2,3', const {'a': 'z', '100': 1, '20': 2, '3': 3});
  check('a,100,3,20', 'z,1,3,2', const {'a': 'z', '100': 1, '3': 3, '20': 2});
  check('a,20,100,3', 'z,2,1,3', const {'a': 'z', '20': 2, '100': 1, '3': 3});
  check('a,20,3,100', 'z,2,3,1', const {'a': 'z', '20': 2, '3': 3, '100': 1});
  check('a,3,100,20', 'z,3,1,2', const {'a': 'z', '3': 3, '100': 1, '20': 2});
  check('a,3,20,100', 'z,3,2,1', const {'a': 'z', '3': 3, '20': 2, '100': 1});

  // Records.
  check(
      '(1, 1),(2, 2),(3, 3)', '1,2,3', const {(1, 1): 1, (2, 2): 2, (3, 3): 3});
  check(
      '(1, 1),(3, 3),(2, 2)', '1,3,2', const {(1, 1): 1, (3, 3): 3, (2, 2): 2});
  check(
      '(2, 2),(1, 1),(3, 3)', '2,1,3', const {(2, 2): 2, (1, 1): 1, (3, 3): 3});
  check(
      '(2, 2),(3, 3),(1, 1)', '2,3,1', const {(2, 2): 2, (3, 3): 3, (1, 1): 1});
  check(
      '(3, 3),(1, 1),(2, 2)', '3,1,2', const {(3, 3): 3, (1, 1): 1, (2, 2): 2});
  check(
      '(3, 3),(2, 2),(1, 1)', '3,2,1', const {(3, 3): 3, (2, 2): 2, (1, 1): 1});

  // Same as above, without `const`.

  check('', '', {});
  check('1,2', '10,20', {1: 10, 2: 20});
  check('2,1', '20,10', {2: 20, 1: 10});

  // Integer keys.
  check('100,20,3', '1,2,3', {100: 1, 20: 2, 3: 3});
  check('100,3,20', '1,3,2', {100: 1, 3: 3, 20: 2});
  check('20,100,3', '2,1,3', {20: 2, 100: 1, 3: 3});
  check('20,3,100', '2,3,1', {20: 2, 3: 3, 100: 1});
  check('3,100,20', '3,1,2', {3: 3, 100: 1, 20: 2});
  check('3,20,100', '3,2,1', {3: 3, 20: 2, 100: 1});

  // Strings with integer-like values.
  check('100,20,3', '1,2,3', {'100': 1, '20': 2, '3': 3});
  check('100,3,20', '1,3,2', {'100': 1, '3': 3, '20': 2});
  check('20,100,3', '2,1,3', {'20': 2, '100': 1, '3': 3});
  check('20,3,100', '2,3,1', {'20': 2, '3': 3, '100': 1});
  check('3,100,20', '3,1,2', {'3': 3, '100': 1, '20': 2});
  check('3,20,100', '3,2,1', {'3': 3, '20': 2, '100': 1});

  check('100,200,30,40,5,6', '1,2,3,4,5,6',
      {'100': 1, '200': 2, '30': 3, '40': 4, '5': 5, '6': 6});
  check('6,5,40,30,200,100', '6,5,4,3,2,1',
      {'6': 6, '5': 5, '40': 4, '30': 3, '200': 2, '100': 1});
  check('5,6,30,40,100,200', '5,6,3,4,1,2',
      {'5': 5, '6': 6, '30': 3, '40': 4, '100': 1, '200': 2});
  check('5,6,100,200,30,40', '5,6,1,2,3,4',
      {'5': 5, '6': 6, '100': 1, '200': 2, '30': 3, '40': 4});

  // String keys, mixed integer-like and other.
  check('100,20,3,a', '1,2,3,z', {'100': 1, '20': 2, '3': 3, 'a': 'z'});
  check('100,3,20,a', '1,3,2,z', {'100': 1, '3': 3, '20': 2, 'a': 'z'});
  check('20,100,3,a', '2,1,3,z', {'20': 2, '100': 1, '3': 3, 'a': 'z'});
  check('20,3,100,a', '2,3,1,z', {'20': 2, '3': 3, '100': 1, 'a': 'z'});
  check('3,100,20,a', '3,1,2,z', {'3': 3, '100': 1, '20': 2, 'a': 'z'});
  check('3,20,100,a', '3,2,1,z', {'3': 3, '20': 2, '100': 1, 'a': 'z'});

  check('a,100,20,3', 'z,1,2,3', {'a': 'z', '100': 1, '20': 2, '3': 3});
  check('a,100,3,20', 'z,1,3,2', {'a': 'z', '100': 1, '3': 3, '20': 2});
  check('a,20,100,3', 'z,2,1,3', {'a': 'z', '20': 2, '100': 1, '3': 3});
  check('a,20,3,100', 'z,2,3,1', {'a': 'z', '20': 2, '3': 3, '100': 1});
  check('a,3,100,20', 'z,3,1,2', {'a': 'z', '3': 3, '100': 1, '20': 2});
  check('a,3,20,100', 'z,3,2,1', {'a': 'z', '3': 3, '20': 2, '100': 1});

  // Records.
  check('(1, 1),(2, 2),(3, 3)', '1,2,3', {(1, 1): 1, (2, 2): 2, (3, 3): 3});
  check('(1, 1),(3, 3),(2, 2)', '1,3,2', {(1, 1): 1, (3, 3): 3, (2, 2): 2});
  check('(2, 2),(1, 1),(3, 3)', '2,1,3', {(2, 2): 2, (1, 1): 1, (3, 3): 3});
  check('(2, 2),(3, 3),(1, 1)', '2,3,1', {(2, 2): 2, (3, 3): 3, (1, 1): 1});
  check('(3, 3),(1, 1),(2, 2)', '3,1,2', {(3, 3): 3, (1, 1): 1, (2, 2): 2});
  check('(3, 3),(2, 2),(1, 1)', '3,2,1', {(3, 3): 3, (2, 2): 2, (1, 1): 1});
}
